<%@page import="java.util.Date"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="../include/tag.jsp"%>
<jsp:useBean id="now" class="java.util.Date" scope="page"/>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>短信推送</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../include/commonFile.jsp"%>
</head><body>
<!--头部-->
<%@include file="../include/header.jsp"%>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp"%>
    <!--内容-->
    <section>
        <div class="section-main">
            <input type="hidden" id="targetId" value="${targetId}"/>
            <div class="layui-tab-item layui-show">
                <div class="layui-tab layui-tab-brief" lay-filter="type">
                    <ul class="layui-tab-title" id="status_tab">
                        <li <c:if test="${sendStatus == 0}">class="layui-this"</c:if>>待发送</li>
                        <li <c:if test="${sendStatus == 1}">class="layui-this"</c:if>>已发送</li>
                    </ul>
                    <div class="layui-tab-content">
                        <form class="layui-form" action="${ctx}/notify/template/list.do?targetId=${targetTemplateWithQuery.targetId}" id="myForm" method="post">
                            <input type="hidden" name="pageNo" id="pageNo" />
                            <input type="hidden" name="sendStatus" id="sendStatus" value="${sendStatus}"/>
                            <div class="f-search-bar">
                                <div class="search-container">
                                    <ul class="search-form-content">
                                        <li class="form-item"><label class="search-form-lable">发送类型</label>
                                            <div class="check-btn-inner">
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),'','#myForm')" ${empty targetTemplateWithQuery.type ? 'class="active"' : ''}>全部</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),3,'#myForm')" ${targetTemplateWithQuery.type == 3 ? 'class="active"' : ''}>所有众筹</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),1,'#myForm')" ${targetTemplateWithQuery.type == 1 ? 'class="active"' : ''}>众筹进行中</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),0,'#myForm')" ${targetTemplateWithQuery.type == 0 ? 'class="active"' : ''}>众筹成功</a>
                                                <input type="hidden" name="type" value="${targetTemplateWithQuery.type}" />
                                            </div>
                                        </li>
                                    </ul>
                                    <ul class="search-form-content">
                                        <li class="form-item"><label class="search-form-lable">发送时间</label>
                                            <div class="check-btn-inner">
                                                <a id="all" href="javascript:void(0);" onclick="setTimeType($(this),0,'#myForm')" ${empty targetTemplateWithQuery.timeType || targetTemplateWithQuery.timeType == 0 ? 'class="active"' : ''}>全部</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),1,'#myForm')" ${targetTemplateWithQuery.timeType == 1 ? 'class="active"' : ''}>今天</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),2,'#myForm')" ${targetTemplateWithQuery.timeType == 2 ? 'class="active"' : ''}>本周内</a>
                                                <a href="javascript:void(0);" onclick="setTimeType($(this),3,'#myForm')" ${targetTemplateWithQuery.timeType == 3 ? 'class="active"' : ''}>本月内</a>
                                                <input type="hidden" name="timeType" value="${targetTemplateWithQuery.timeType}" />
                                            </div>
                                            <div class="layui-inline">
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="createStart" value="${targetTemplateWithQuery.createStart}" placeholder="开始日"

                                                    >
                                                </div>
                                                -
                                                <div class="layui-input-inline">
                                                    <input class="layui-input" type="text" name="createEnd" value="${targetTemplateWithQuery.createEnd}" placeholder="截止日"

                                                    >
                                                </div>
                                            </div>
                                        </li>
                                        <li class="form-item-inline">
                                            <div class="sub-btns">
                                                <a class="layui-btn layui-btn-danger" href="javascript:submitFunction('#myForm')">查询</a>
                                                <a class="layui-btn layui-btn-normal" href="javascript:resetFunction('#myForm')">重置</a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </form>
                        <div class="my-act-list-content">
                            <ul class="num">
                                <div class="r">
                                    <li style="cursor: pointer;" class="r">
                                        <a href="javascript:txz.expireCheck('', function() { add('', 'add') })" class="layui-btn layui-btn-danger layui-btn-sm"><i
                                                class="iconfont icon-add btn-icon"></i>新增短信</a>
                                    </li>
                                </div>
                                <p class="cl"></p>
                            </ul>
                            <div class="cl">
                                <table class="layui-table">
                                    <colgroup>
                                        <col width="60px">
                                        <col width="150px">
                                        <col>
                                        <col width="100px">
                                        <col width="160px">
                                        <col width="100px">
                                        <col width="100px">
                                        <col width="300px">
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>发送对象</th>
                                        <th>短信内容</th>
                                        <th>发送状态</th>
                                        <th>发送时间</th>
                                        <th>成功数</th>
                                        <th>失败数</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach var="template" items="${list}" varStatus="status">
                                        <tr>
                                            <td>
                                                    ${status.index + 1}
                                            </td>
                                            <td>
                                                <c:if test="${template.type == 0}"> 众筹成功</c:if>
                                                <c:if test="${template.type == 1}"> 众筹进行中</c:if>
                                                <c:if test="${template.type == 3}"> 所有众筹</c:if>
                                            </td>
                                            <td>
                                                    ${template.template}
                                            </td>
                                            <td>
                                                <c:choose>
                                                    <c:when test="${template.sendTime < now}">
                                                        已发送
                                                    </c:when>
                                                    <c:otherwise>
                                                        待发送
                                                    </c:otherwise>
                                                </c:choose>
                                            </td>
                                            <td>
                                                    <%--<c:if test="${empty template.sendTime}"><fmt:formatDate value="${template.createDate}" pattern="yyyy-MM-dd HH:mm" /></c:if>--%>
                                                <c:if test="${!empty template.sendTime}"><fmt:formatDate value="${template.sendTime}" pattern="yyyy-MM-dd HH:mm" /></c:if>
                                            </td>
                                            <td>
                                                <span style="text-decoration:underline; cursor: pointer;" data-id="${template.id}" class="successNum">${template.successNum}</span>
                                            </td>
                                            <td>
                                                <span style="text-decoration:underline; cursor: pointer;" data-id="${template.id}" class="failureNum">${template.failureNum}</span>
                                            </td>
                                            <td  class="tb-opts">
                                                <div class="comm-opts">
                                                    <c:if test="${empty template.sendTime or now > template.sendTime}">
                                                        <%--<a href="javascript:send('${template.id}')" target="_self">
                                                            立即发送
                                                        </a>--%>
                                                        <a href="javascript:txz.expireCheck('', function() { add('', 'setTime', '${template.id}') })" target="_self">
                                                            再次发送
                                                        </a>
                                                    </c:if>
                                                    <c:if test="${!empty template.sendTime and now < template.sendTime}">
                                                        <a href="javascript:txz.expireCheck('', function() { add('', 'modifyTime', '${template.id}') })" target="_self">
                                                            修改发送时间
                                                        </a>
                                                    </c:if>
                                                    <a href="javascript:txz.deleteObject('确定删除该短信么？', '${ctx}/crowdfund/target/delQuartz.do?id=${template.id}')" target="_self">
                                                        删除
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                    </tbody>
                                </table>
                                <div id="page_content" class="page-container"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!--底部-->
        <%@include file="../include/footer.jsp"%>
    </section>
</div>

<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '众筹项目',
            href:'${ctx}/activity/activity/zcActivityList.do'
        },{
            name: '${activity.title}'
        },{
            name: '短信推送',
            curr: true
        }],
        info: '<span class="f12">共<b>${page.totalCount}</b>条记录</span>',
        btns:[{
            type:'back'
        }]
    });
    $(function () {
        //加载分页
        loadPageNew({elem: 'page_content',totalCount: '${page.totalCount}',limit: '${page.limit}',page: '${page.page}'});
        layui.use(['laydate', 'element'],function () {
            var laydate = layui.laydate;
            var start = {
                elem: 'input[name=createStart]'
            };

            var end = {
                elem: 'input[name=createEnd]'
            };
            laydate.render(start);
            laydate.render(end);

            var element = layui.element;
            element.on('tab(type)', function (data) {
                location.href = "${ctx}/notify/template/list.do?targetId=" + $('#targetId').val() + "&sendStatus=" + data.index;
            });
        })
    })

    function send(id) {
        layer.confirm('确定要发送短信吗？', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            layer.close(index);
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.post('${ctx}/crowdfund/target/sendForCrowdfund.do', {id: id}, function(res) {
                layer.close(loadIndex);
                if (res.success) {
                    util.layerMsgSuccess('发送成功', function () {
                        location.reload();
                    });
                } else {
                    util.layerMsgError(res.description)
                }
            });
        });
    }

    $('.successNum').click(function () {
        var id = this.dataset.id;
        window.location.href = '${ctx}/notify/message/listAll.do?templateId='+ id + '&isSuccess=1';
    });

    $('.failureNum').click(function () {
        var id = this.dataset.id;
        window.location.href = '${ctx}/notify/message/listAll.do?templateId='+ id + '&isSuccess=0';
    });

    function add(target, behavior, id) {
        layer.open({
            type: 2,
            area: ['900px', '650px'],
            title: '新增短信',
            maxmin: true, //开启最大化最小化按钮
            content: '${ctx}/crowdfund/target/messageView.do?targetId=${targetTemplateWithQuery.targetId}&behavior='
            + behavior + '&id=' + id,
            btn: ['确定发送', /*'定时发送', */'关闭'],
            /*yes: function (index, layero) {
                if (behavior != 'add') {
                    return false;
                }
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                if (target) {
                    top_iframe = target;//如果指定了iframe，则在改frame中跳转
                } else {
                    top_iframe = '_parent';//获取当前active的tab的iframe
                }
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                if (iframeWin.contentWindow.doSubmit()) {
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);//延时0.1秒，对应360 7.1版本bug

                    setTimeout(function () {
                        window.location.reload();
                    }, 200);
                }
            },*/
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                if (target) {
                    top_iframe = target;//如果指定了iframe，则在改frame中跳转
                } else {
                    top_iframe = '_parent';//获取当前active的tab的iframe
                }
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                if (iframeWin.contentWindow.submitQuartz()) {
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);//延时0.1秒，对应360 7.1版本bug

                    setTimeout(function () {
                        window.location.reload();
                    }, 200);
                } else {
                    return false;
                }
            },
            cancel: function (index) {
            }
        });
    }
</script>
</body>
</html>